package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.domain.Login;
import cn.itsource.ymcc.mapper.LoginMapper;
import cn.itsource.ymcc.result.JSONResult;
import cn.itsource.ymcc.service.ILoginService;
import cn.itsource.ymcc.util.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author Eric Chen
 * @since 2022-07-07
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {
    private String url = "http://localhost:10040/oauth/token?client_id=%s&client_secret=%s&grant_type=password&username=%s&password=%s";
    @Override
    public JSONResult common(Login login) {
        String username = login.getUsername();
        String password = login.getPassword();

        EntityWrapper<Login> wrapper = new EntityWrapper<>();
        wrapper.eq("username",username);
        Login login1 = selectOne(wrapper);


        String clientId = login1.getClientId();
        String clientSecret = login1.getClientSecret();
        String.format(url,clientId,clientSecret,username,password);
        //发送请求
        String s = HttpUtil.sendPost(url, null);
        Map<String,Object> stringObjectMap = JSON.parseObject(s,Map.class);
        return JSONResult.success(stringObjectMap);
    }
}
